﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.CraftingPieceCollectionElementViewExtensions
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public static class CraftingPieceCollectionElementViewExtensions
  {
    public static MatrixFrame GetCraftingPieceFrameForInventory(this CraftingPiece craftingPiece)
    {
      MatrixFrame identity1 = MatrixFrame.Identity;
      Mat3 identity2 = Mat3.Identity;
      float num1 = 0.85f;
      Vec3 vec3_1 = new Vec3();
      MetaMesh copy = MetaMesh.GetCopy(craftingPiece.MeshName);
      if ((NativeObject) copy != (NativeObject) null)
      {
        identity2.RotateAboutSide(-1.57079637f);
        identity2.RotateAboutForward(-0.7853982f);
        Vec3 v1_1 = new Vec3(1000000f, 1000000f, 1000000f);
        Vec3 v1_2 = new Vec3(-1000000f, -1000000f, -1000000f);
        for (int meshIndex = 0; meshIndex != copy.MeshCount; ++meshIndex)
        {
          Vec3 boundingBoxMin = copy.GetMeshAtIndex(meshIndex).GetBoundingBoxMin();
          Vec3 boundingBoxMax = copy.GetMeshAtIndex(meshIndex).GetBoundingBoxMax();
          Vec3[] vec3Array = new Vec3[8]
          {
            identity2.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMin.y, boundingBoxMin.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMin.y, boundingBoxMax.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMax.y, boundingBoxMin.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMax.y, boundingBoxMax.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMin.y, boundingBoxMin.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMin.y, boundingBoxMax.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMax.y, boundingBoxMin.z)),
            identity2.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMax.y, boundingBoxMax.z))
          };
          for (int index = 0; index < 8; ++index)
          {
            v1_1 = Vec3.Vec3Min(v1_1, vec3Array[index]);
            v1_2 = Vec3.Vec3Max(v1_2, vec3Array[index]);
          }
        }
        float num2 = 1f;
        Vec3 vec3_2 = (v1_1 + v1_2) * 0.5f;
        float num3 = MathF.Max(v1_2.x - v1_1.x, v1_2.y - v1_1.y);
        float scaleAmount = num1 * num2 / num3;
        identity1.origin -= vec3_2 * scaleAmount;
        identity1.origin += vec3_1;
        identity1.rotation = identity2;
        identity1.rotation.ApplyScaleLocal(scaleAmount);
        identity1.origin.z -= 5f;
      }
      return identity1;
    }
  }
}
